<template>
  <view class="container">
    <header @click="$goPath(1)">
      <image src='/static/images/right-white.png' mode='aspectFit|aspectFill|widthFix' alt="" />
    </header>
    <section>
      <image
        src='https://youpindou.com:8083/doudougou/image/showImg?imgUrl=/home/ddg_test/images/5d58cd43-eba0-45ae-a11c-50ce90a43faf.png'
        mode='widthFix' alt="" />
      <view class="sect-input" v-if="!userDetail.parent">
        <u-input placeholder="请输入绑定的邀请码" shape="circle" v-model="code"></u-input>

        <view>注意：一个账号只允许绑定一个邀请码，绑定成功不可修改。</view>
      </view>

      <view class="sect-info" v-else>
        当前绑定的ID号为：<text>{{ userDetail.parent }}</text>
      </view>
    </section>

    <footer v-if="!userDetail.parent">
      <view @click="onBind">立即绑定</view>
    </footer>

    <uni-popup ref="bindPopup" style="z-index: 1000;">
      <view class='modal-confirm' @click.stop>
        <view class="confirm-head">
          <image src='https://youpindou.com/miniProgram/image/showImg?imgUrl=/home/ypd/images/bind-successIcon.png' mode='widthFix' alt="" />
        </view>
        <view class="confirm-title">绑定成功</view>
        <view>当前绑定的ID号为：<text>{{ userDetail.parent }}</text></view>
        <view class="confirm-btn" @click="handleClose">知道了</view>
      </view>
    </uni-popup>

  </view>
</template>
<script>
// import { user_bindParent, user_information } from '../../service/api.js';
import {getUserInfo,setUserInfo} from '@/common/auth';

export default {
  data() {
    return {
      code: '',
      userDetail: {}
    }
  },

  methods: {
    //获取个人信息
     getUserDetail() {
      let info = getUserInfo()
      this.$request.userInformation({ userId: info.userId }).then(res => {
        const { code, isSuccess, data } = res;
        if (code == 200 && isSuccess) {
          this.userDetail = data;
          setUserInfo(data)
        } else {
          uni.showToast({ title: res.msg, icon: 'none' });
        };
      });

    },

    //立即绑定
    onBind() {
      const { code, userDetail } = this;
      if(!code) return this.$toast('请输入邀请码')
      this.$request.userBindParent({ code, userId: userDetail.userId }).then(res => {
        if (res.code == 200 && res.isSuccess) {
          this.getUserDetail();
          this.$refs.bindPopup.open('center');
        } else {
          uni.showToast({ title: res.msg, icon: 'none' });
        }
      })
    },

    //知道了
    handleClose() {
      this.$refs.bindPopup.close();
    },

  },

  onShow() {
    this.getUserDetail();
  },
}
</script>

<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100vh;
  font-size: 28rpx;
  font-family: 'PingFang SC-Medium';
  background-color: #fff;
  overflow-y: auto;
  display: flex;
  flex-direction: column;

  header {
    width: 100%;
    position: fixed;
    top: 6%;
    left: 4%;
    z-index: 100;

    image {
      width: 16rpx;
        height: 28rpx;
        display: block;
        margin-right: 12rpx;
    }
  }

  section {
    width: 100%;
    height: 100%;
    overflow-y: auto;
    padding-bottom: 140rpx;
    box-sizing: border-box;
    position: relative;

    image {
      width: 100%;
      height: auto;
      display: block;
    }

    .sect-input {
      padding: 0 24rpx;
      margin-top: 24rpx;
      display: flex;
      flex-direction: column;
      align-items: center;

      .u-input {
        width: 90%;
        border-color: #32B499 !important;
        padding-left: 30rpx !important;
      }

      view {
        font-size: 24rpx;
        font-family: 'PingFang SC-Regular';
        color: #999999;
        margin-top: 24rpx;
      }
    }

    .sect-info {
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 32rpx;
      font-family: 'DIN-Medium';
    }

  }

  footer {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    padding: 24rpx 48rpx;
    box-sizing: border-box;
    background-color: #fff;
    box-shadow: 0px -4rpx 8rpx 0px rgba(51, 51, 51, 0.1);
	z-index: 99;
    view {
      width: 100%;
      height: 80rpx;
      background-color: #32B499;
      display: flex;
      justify-content: center;
      align-items: center;
      color: #fff;
      border-radius: 40rpx;
    }
  }

  .modal-confirm {
    width: 480rpx;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background-color: #fff;
    border-radius: 20rpx;
    height: auto;
    font-size: 28rpx;
    color: #333;
    padding-bottom: 24rpx;
    box-sizing: border-box;

    .confirm-head {
      width: 100%;

      image {
        width: 100%;
        height: auto;
        display: block;
      }
    }

    .confirm-title {
      font-size: 36rpx;
      font-family: 'PingFang SC-Bold';
      font-weight: bold;
      color: #333333;
      margin: 24rpx 0;
    }

    .confirm-btn {
      width: 360rpx;
      height: 80rpx;
      background: #32B499;
      border-radius: 40rpx;
      display: flex;
      justify-content: center;
      align-items: center;
      margin-top: 24rpx;
      color: #fff;
    }
  }

}
</style>
